// 1. 存入个人信息 (token, userId)
// 2. 搜索历史记录 (searchHistory)
// 3. 省市区地址 (address)

// 将Vuex中的数据持久化存储到本地存储中
// 约定一个通用的键名 (Vue持久化存储)
const INFO_KEY = 'hm_shopping_info' // 个人信息
const HISTORY_KEY = 'hm_shopping_search_history' // 搜索历史
const ADDRESS_KEY = 'hm_shopping_address' // 省市区地址

// 获取个人信息
export const getInfo = () => {
  return localStorage.getItem(INFO_KEY) ? JSON.parse(localStorage.getItem(INFO_KEY)) : { token: '', userId: '' }
}

// 设置个人信息
export const setInfo = (obj) => {
  localStorage.setItem(INFO_KEY, JSON.stringify(obj))
}

// 移除个人信息
export const removeInfo = () => {
  localStorage.removeItem(INFO_KEY)
}

// --------------------------------------------------------------------------------------------------------------------------

// 获取搜索历史
export const getHistory = () => {
  return localStorage.getItem(HISTORY_KEY) ? JSON.parse(localStorage.getItem(HISTORY_KEY)) : []
}

// 设置搜索历史
export const setHistory = (arr) => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(arr))
}

// 移除搜索历史
export const removeHistory = () => {
  localStorage.removeItem(HISTORY_KEY)
}

// --------------------------------------------------------------------------------------------------------------------------

// 获取省市区地址
export const getAddress = () => {
  return localStorage.getItem(ADDRESS_KEY) ? JSON.parse(localStorage.getItem(ADDRESS_KEY)) : []
}

// 设置省市区地址
export const setAddress = (arr) => {
  localStorage.setItem(ADDRESS_KEY, JSON.stringify(arr))
}

// 移除省市区地址
export const removeAddress = () => {
  localStorage.removeItem(ADDRESS_KEY)
}
